package com.c.yinyuezhushou.Fragment;

import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.arialyy.aria.core.Aria;
import com.arialyy.aria.core.download.DownloadEntity;
import com.c.yinyuezhushou.Adapter.DownRecAdapter1;
import com.c.yinyuezhushou.Adapter.DownRecAdapter2;
import com.c.yinyuezhushou.MyApplication;
import com.c.yinyuezhushou.R;
import com.c.yinyuezhushou.Service.DownService;

import java.io.File;
import java.util.List;

public class DownFragment extends Fragment {
    private RecyclerView recyclerView1;
    private RecyclerView recyclerView2;
    private DownService downService;
    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View rootView = inflater.inflate(R.layout.fragment_down, container, false);
        recyclerView1 = rootView.findViewById(R.id.fd_rec1);
        recyclerView2 = rootView.findViewById(R.id.fd_rec2);
        return rootView;
    }

    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        MyApplication myApplication = (MyApplication) getActivity().getApplicationContext();
        downService = myApplication.getDownService();
        List<DownloadEntity> DownloadEntity= downService.getDownloadEntity();

        for (DownloadEntity downloadEntities: DownloadEntity){
            if (!new File(downloadEntities.getFilePath()).exists()){
                Aria.download(this).load(downloadEntities.getId()).cancel();
            }
        }
        LinearLayoutManager linearLayoutManager1 = new LinearLayoutManager(getActivity());
        LinearLayoutManager linearLayoutManager2 = new LinearLayoutManager(getActivity());
        recyclerView1.setLayoutManager(linearLayoutManager1);
        recyclerView2.setLayoutManager(linearLayoutManager2);
        DownRecAdapter2 downRecAdapter2 = new DownRecAdapter2(downService.getDownloadEntity(),downService);
        recyclerView1.setAdapter(new DownRecAdapter1(downService.getNoDownloadEntity(),downService,downRecAdapter2));
        recyclerView2.setAdapter(downRecAdapter2);

    }


}
